﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AppSocketModel
{

    /// <summary>
    /// 枚举扩展
    /// </summary>
    public static class EnumExtension
    {
        /// <summary>
        /// 获取枚举的显示名称。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDisplayString(this Enum value)
        {
            DescriptionAttribute attribute = value.GetType().GetTypeInfo().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;

            if (attribute != null)
            {
                return attribute.Description.IndexOf('|') != -1 ? attribute.Description.Split('|')[0] : attribute.Description;
            }

            return value.ToString();
        }

        /// <summary>
        /// 获取枚举的说明文字。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescriptionString(this Enum value)
        {
            DescriptionAttribute attribute = value.GetType().GetTypeInfo().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;

            if (attribute != null)
            {
                return attribute.Description.IndexOf('|') != -1 ? attribute.Description.Split('|')[1] : attribute.Description;
            }

            return value.ToString();
        }
    }
}
